import request from './request'
import type { ApiResponse } from '@/types/api'
import type { PageResponse } from '@/types/common'
import type {
  Team,
  TeamMember,
  TeamInviteCode,
  CreateTeamRequest,
  UpdateTeamRequest,
  AddMemberRequest,
  UpdateMemberRoleRequest,
  GenerateInviteCodeRequest,
  TeamSettings,
  TeamSettingsRequest
} from '@/types/team'

// 团队管理API
export const teamApi = {
  // 创建团队
  createTeam(data: CreateTeamRequest): Promise<ApiResponse<Team>> {
    return request.post('/teams', data)
  },

  // 获取我的团队列表
  getMyTeams(): Promise<ApiResponse<Team[]>> {
    return request.get('/teams/my')
  },

  // 获取我创建的团队数量
  getMyCreatedTeamCount(): Promise<ApiResponse<number>> {
    return request.get('/teams/my/created/count')
  },

  // 获取团队详情
  getTeamDetail(teamId: number): Promise<ApiResponse<Team>> {
    return request.get(`/teams/${teamId}`)
  },

  // 更新团队信息
  updateTeam(teamId: number, data: UpdateTeamRequest): Promise<ApiResponse<Team>> {
    return request.put(`/teams/${teamId}`, data)
  },

  // 删除团队
  deleteTeam(teamId: number): Promise<ApiResponse<void>> {
    return request.delete(`/teams/${teamId}`)
  },

  // 审核团队 (管理员)
  approveTeam(teamId: number, approved: boolean): Promise<ApiResponse<Team>> {
    return request.put(`/teams/${teamId}/approve`, { approved })
  },

  // 获取待审核团队列表 (管理员)
  getPendingTeams(params: {
    page?: number
    size?: number
    sortBy?: string
    sortDir?: string
  } = {}): Promise<ApiResponse<PageResponse<Team>>> {
    return request.get('/teams/pending', { params })
  },

  // 转移团队所有权
  transferOwnership(teamId: number, newOwnerId: number): Promise<ApiResponse<TeamMember>> {
    return request.put(`/teams/${teamId}/transfer-ownership`, { newOwnerId })
  },

  // 获取团队成员在线状态
  getTeamMembersOnlineStatus(teamId: number): Promise<ApiResponse<{
    teamId: number
    members: Array<{
      userId: number
      nickname?: string
      username: string
      role: string
      joinTime: string
      isOnline: boolean
      lastActiveTime?: string
      status: string
    }>
    totalMembers: number
    onlineMembers: number
    updateTime: string
  }>> {
    return request.get(`/teams/${teamId}/members/online-status`)
  },

  // 获取团队设置
  getTeamSettings(teamId: number): Promise<ApiResponse<TeamSettings>> {
    return request.get(`/teams/${teamId}/settings`)
  },

  // 更新团队设置
  updateTeamSettings(teamId: number, data: TeamSettingsRequest): Promise<ApiResponse<TeamSettings>> {
    return request.put(`/teams/${teamId}/settings`, data)
  },

  // 检查团队可见性
  checkTeamVisibility(teamId: number): Promise<ApiResponse<{
    teamId: number
    isVisible: boolean
    isMember: boolean
    userId: number
  }>> {
    return request.get(`/teams/${teamId}/visibility`)
  }
}

// 团队成员管理API
export const teamMemberApi = {
  // 获取团队成员列表
  getTeamMembers(teamId: number): Promise<ApiResponse<TeamMember[]>> {
    return request.get(`/teams/${teamId}/members`)
  },

  // 添加团队成员
  addTeamMember(teamId: number, data: AddMemberRequest): Promise<ApiResponse<TeamMember>> {
    return request.post(`/teams/${teamId}/members`, data)
  },

  // 移除团队成员
  removeTeamMember(teamId: number, userId: number): Promise<ApiResponse<void>> {
    return request.delete(`/teams/${teamId}/members/${userId}`)
  },

  // 更新成员角色
  updateMemberRole(teamId: number, userId: number, data: UpdateMemberRoleRequest): Promise<ApiResponse<TeamMember>> {
    return request.put(`/teams/${teamId}/members/${userId}`, data)
  }
}

// 团队邀请码API
export const teamInviteApi = {
  // 生成团队邀请码
  generateInviteCode(teamId: number, data: GenerateInviteCodeRequest): Promise<ApiResponse<TeamInviteCode>> {
    return request.post(`/teams/${teamId}/invite-code`, data)
  },

  // 获取团队邀请码列表
  getTeamInviteCodes(teamId: number): Promise<ApiResponse<TeamInviteCode[]>> {
    return request.get(`/teams/${teamId}/invite-codes`)
  },

  // 通过邀请码加入团队
  joinTeamByInviteCode(inviteCode: string): Promise<ApiResponse<TeamMember>> {
    return request.post(`/teams/join/${inviteCode}`)
  },

  // 禁用邀请码
  disableInviteCode(teamId: number, codeId: number): Promise<ApiResponse<void>> {
    return request.put(`/teams/${teamId}/invite-codes/${codeId}/disable`)
  },

  // 刷新邀请码
  refreshInviteCode(teamId: number, codeId: number): Promise<ApiResponse<TeamInviteCode>> {
    return request.put(`/teams/${teamId}/invite-codes/${codeId}/refresh`)
  }
}

// 统一导出
export default {
  ...teamApi,
  ...teamMemberApi,
  ...teamInviteApi
}
